﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Text
Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.OleDb

'This class is used to display comments in the beach details page and messages in the profile page.
Public Class Comments

    Private Shared sb As New StringBuilder()
    Public Shared Sub GetComments(ByVal pgID As Integer, ByVal literalCtr As Literal)
        GetComments(0, pgID, literalCtr)
        ' Get comments of the fist level ( comments wich is not reply to other comment)
        Dim retString = sb.ToString()
        sb.Clear()
        literalCtr.Text = retString
    End Sub

    ' This method displays the comments in the beach detail page.
    ' Params : replyTo    - The level of reply (if its 0 then it is a comment, if its > 0 then it is a reply to a comment)
    '          pgID       - The ID of the beach that this comment refers to.
    '          literalCtr - The control which is used to display these comments
    Private Shared Sub GetComments(ByVal replyTo As Integer, ByVal pgID As Integer, ByVal literalCtr As Literal)
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT Id , Beaches_FK, Title, CommentsName, CommentsBody, DateAdded FROM Comments WHERE Beaches_FK = @beaches_FK AND replyTo = @ReplyTo ORDER BY Id DESC"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.Parameters.AddWithValue("@beaches_FK", pgID)
        cmd.Parameters.AddWithValue("@ReplyTo", replyTo)
        oleDbConn.Open()
        Using dr = cmd.ExecuteReader()
            While dr.Read()
                'Get time difference for comment (Now - when added)
                Dim mytimeago As String = ""
                mytimeago = CalculateTime.calculateTime(dr("DateAdded"))
                ' Open comment wrapper div
                sb.Append("<div class='CommentWrapper'>")
                ' If it a reply (not 0) move comment 25 pixels further to display it as reply. 
                If replyTo <> 0 Then
                    sb.Append("<div class='Arrow'></div>")
                End If
                ' Open the comment div, adding name with link, add a reply link, add time  
                ' Add the comment and reply then close the comment div 
                sb.Append("<div class='Comment'><label>Name :</label><a class='red' href='Profile.aspx?Tourist=" & Convert.ToString(dr("CommentsName")) & "'>" & Convert.ToString(dr("CommentsName")) & "</a>" & "•&nbsp;&nbsp;<a href='AddReply.aspx?replyTo=" & Convert.ToString(dr("Id")) & "&Beaches_FK=" & Convert.ToString(dr("Beaches_FK")) & "'>Reply</a> &nbsp;•&nbsp;<span id='grey'>" & mytimeago & "  </span><br/><span id='Orange'><i>Title</i> :</span> " & dr("Title").ToString() & "<br/>" & dr("CommentsBody").ToString() & "</div>")
                ' Call get comments ( reply comments for the current comment)
                GetComments(Integer.Parse(dr("Id").ToString()), pgID, literalCtr)
                ' finally close the  comment wrapper tag .
                sb.Append("</div>")
            End While
        End Using
        oleDbConn.Close()
    End Sub

    Public Shared Sub GetMessages(ByVal touristName As String, ByVal literalCtr As Literal)
        GetMessagesNow(touristName, literalCtr)
        ' Get messages of the first level
        Dim retString = sb.ToString()
        sb.Clear()
        literalCtr.Text = retString
    End Sub

    ' This method displays the messages in the profile page.
    ' Params : touristName       - The name of the member that this message refers to.
    '          literalCtr - The control which is used to display these comments
    Private Shared Sub GetMessagesNow(ByVal touristName As String, ByVal literalCtr As Literal)
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT Id , TouristName, Title, CommentsName, CommentsBody, DateAdded FROM Personal_Messages WHERE TouristName = @TouristName_FK ORDER BY Id DESC"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.Parameters.AddWithValue("@TouristName_FK", touristName)
        oleDbConn.Open()
        Using dr = cmd.ExecuteReader()
            While dr.Read()
                Dim mytimeago As String = ""
                mytimeago = CalculateTime.calculateTime(dr("DateAdded"))
                ' Open messages wrapper div 
                sb.Append("<div class='ContentWraperMessages'>")
                ' Open the comment div, adding name with link, add a reply link, add time  
                ' Add the comment then close the comment div 
                sb.Append("<div class='Comment'><label> Name : </label> <a class='red' href='Profile.aspx?Tourist=" & Convert.ToString(dr("CommentsName")) & "'>" & Convert.ToString(dr("CommentsName")) & "</a>•&nbsp; &nbsp;<span id='grey'>" & mytimeago & "</span>  <br/><span id='Orange'><i>Title</i> : </span>" & dr("Title").ToString() & "<br/>" & dr("CommentsBody").ToString() & "</div>")
                ' finally close the  comment wrapper tag .
                sb.Append("</div>")
            End While
        End Using
        oleDbConn.Close()
    End Sub
End Class
